/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PregnantRecords;
import com.jims.master.modules.doctor.entity.PregnantVisit;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.PregnantRecordsService;
import com.jims.master.modules.doctor.service.PregnantVisitService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 孕妇手册-复诊记录Controller
 * @author 王志明
 * @version 2017-07-21
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/pregnantVisit")
public class PregnantVisitController extends BaseController {

	@Autowired
	private PregnantVisitService pregnantVisitService;
    @Autowired
    private ClinicMasterService clinicMasterService;
    @Autowired
    private PregnantRecordsService pregnantRecordsService;
	@ModelAttribute
	public PregnantVisit get(@RequestParam(required=false) String id) {
		PregnantVisit entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pregnantVisitService.get(id);
		}
		if (entity == null){
			entity = new PregnantVisit();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(PregnantVisit pregnantVisit,String clinicId,String title,Integer num, HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        pregnantVisit.setClinicId(clinicId);
        if (title !=null&&!title.equals("")){
            pregnantVisit.setGestationalTitle(Integer.valueOf(title));
        }
        if(num!=null&&!num.equals("")){
            pregnantVisit.setVisitInt(num);
        }
        Page<PregnantVisit> page = pregnantVisitService.findPage(new Page<PregnantVisit>(request, response), pregnantVisit);
		model.addAttribute("page", page);
        model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/pregnantManual/pregnantVisitIndex";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"expertIndex", ""})
	public String expertList(PregnantVisit pregnantVisit,String clinicId,String title,Integer num, HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        pregnantVisit.setClinicId(clinicId);
        if (title !=null&&!title.equals("")){
            pregnantVisit.setGestationalTitle(Integer.valueOf(title));
        }
        if(num!=null&&!num.equals("")){
            pregnantVisit.setVisitInt(num);
        }
        Page<PregnantVisit> page = pregnantVisitService.findPage(new Page<PregnantVisit>(request, response), pregnantVisit);
		model.addAttribute("page", page);
        model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/pregnantManual/pregnantExpertIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(PregnantVisit pregnantVisit, Model model) {
		model.addAttribute("pregnantVisit", pregnantVisit);
		return "modules/pregnantvisit/pregnantVisitForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PregnantVisit pregnantVisit, Model model) {
	    StringData data=new StringData();
        if(pregnantVisit.getType().equals("1")){
            if(pregnantVisit.getHrsPossess().equals("1")){
                pregnantVisit.setHrsReason("");
                pregnantVisit.setHrsScore(null);
            }
            if(pregnantVisit.getReferral().equals("1")){
                pregnantVisit.setReferralCompany("");
                pregnantVisit.setReferralReason("");
            }
        }
        int i =pregnantVisitService.save(pregnantVisit);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(String id) {
	    StringData data=new StringData();
        PregnantVisit pregnantVisit=new PregnantVisit();
        pregnantVisit.setId(id);
		int i=pregnantVisitService.delete(pregnantVisit);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
    //获取就诊次数
	@RequiresPermissions("user")
	@RequestMapping(value = "getVisitNum")
	@ResponseBody
	public Integer getVisitNum(String clinicId,String title) {
        PregnantVisit pregnantVisit=new PregnantVisit();
        pregnantVisit.setClinicId(clinicId);
        pregnantVisit.setGestationalTitle(Integer.valueOf(title));
		int i=pregnantVisitService.getVisitNum(pregnantVisit);
		return i;
	}
    /**
     * @Author chenxy
     * @desc 查看复诊记录
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findById", ""})
    public String findById(String clinicId,String id,String visitNum, String patientId, String type,HttpServletRequest request, HttpServletResponse response, Model model) {
        ClinicMaster clinicMaster = clinicMasterService.get(clinicId);
        PregnantVisit pregnantVisit = this.pregnantVisitService.get(id);
        String orgId = UserUtils.getOrgId();
        String orgName = pregnantRecordsService.getOrgName(orgId);
        String deptName = UserUtils.getUser().getOrgDept().getDeptName();
        String doctorName = UserUtils.getUser().getName();
        if (pregnantVisit == null) {
            pregnantVisit=new PregnantVisit();
            pregnantVisit.setTestingDatetime(new Date());
            pregnantVisit.setGestationalTitle(1);
        }
        pregnantVisit.setInspectionUnit(orgName);
        pregnantVisit.setDeptName(deptName);
        pregnantVisit.setDoctorName(doctorName);
        pregnantVisit.setDoctorUser(UserUtils.getUser().getId());
        model.addAttribute("pregnantVisit", pregnantVisit);
        model.addAttribute("clinicMaster", clinicMaster);
        if(type.equals("1")){
            return "modules/doctor/pregnantManual/pregnantVisitForm";
        }
        else {
            return "modules/doctor/pregnantManual/pregnantExpertForm";
        }
    }
}